import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const key = 'user'

export default new Vuex.Store({
  state: localStorage.getItem('state') ? JSON.parse(localStorage.getItem('state')): {
    user: null,
    openid:'',
    weChatId:'',
    weChatResult:{},
    lang: "zh",
    userNo:"",
  },
  getters: {
    getStorage: function (state) {
      if (!state.user) {
        state.user = JSON.parse(localStorage.getItem(key))
      }
      return state.user
    }
  },
  mutations: {
    addUserInfo (state, value) {
      state.user = value
      localStorage.setItem(key, JSON.stringify(value))
    },
    getUserNo (state, value) {
      state.userNo = value
      localStorage.setItem("userNo", JSON.stringify(value))
    },
    removeUserInfo (state) {
      state.user = null
      localStorage.removeItem(key)
    },
    changeLang(state, value){
      state.locale = value
    }
  },
  actions: {

  }
})
